home *** CD-ROM | disk | FTP | other *** search
- /************************************************************
-
- Transform.c
-
- Preditor 3.0 extension.
-
- Displays a dialog prompting for text. That text is then inserted or removed from
- the beginning or end of all lines in the document, or optionally, all selected lines
- in the document.
-
- © Copyright Evatac Software 1988-1996
- All rights reserved
-
- ************************************************************/
-
- #include "PreditorExtension.h"
-
- #include <SetupA4.h>
- #ifndef THINKC
- #include <A4Stuff.h>
- #else
- #define SetCurrentA4() 0; RememberA4()
- #define SetA4(x) SetUpA4()
- #endif
-
- #ifdef powerc
- ProcInfoType __procinfo = ExtensionUPPInfo;
- #endif
-
- enum {
- kOk = 1,
- kCancel,
- kSelectionOnly,
- kInsert,
- kDelete,
- kBeginning,
- kEnd,
- kTransformString
- };
-
- typedef struct transformInfo {
- Boolean insert;
- Boolean lineStart;
- Boolean selOnly;
- Boolean pad;
-
- unsigned char transformStr[256];
- } transformInfo;
-
- /*
- * transformSetControl
- */
- static void transformSetControl(DialogRef dialog, short item, Boolean flag)
- {
- Rect r;
- Handle hdl;
- short type;
-
- GetDialogItem(dialog, item, &type, &hdl, &r);
- SetCtlValue((ControlHandle) hdl, flag);
- }
-
- static void transformSetButtons(DialogRef dialog, transformInfo *info)
- {
- transformSetControl(dialog, kInsert, info->insert);
- transformSetControl(dialog, kDelete, !info->insert);
-
- transformSetControl(dialog, kBeginning, info->lineStart);
- transformSetControl(dialog, kEnd, !info->lineStart);
-
- transformSetControl(dialog, kSelectionOnly, info->selOnly);
- }
-
- /*
- * transformHandler
- */
- static short transformHandler(
- DialogRef dialog,
- short message,
- short itemHit,
- void *data
- )
- {
- short type;
- Handle hdl;
- Rect r;
- transformInfo *info = (transformInfo *) data;
-
- switch (message) {
-
- case kDlgInit:
- GetDialogItem(dialog, kSelectionOnly, &type, &hdl, &r);
- SetControlValue((ControlHandle) hdl, info->selOnly);
- HiliteControl((ControlHandle) hdl, info->selOnly ? 0 : 0xff);
-
- GetDialogItem(dialog, kTransformString, &type, &hdl, &r);
- SetIText(hdl, info->transformStr);
- SelIText(dialog, kTransformString, 0, 255);
-
- transformSetButtons(dialog, info);
- break;
-
- case kDlgItemHit:
-
- switch (itemHit) {
-
- case kInsert:
- case kDelete:
- info->insert = (itemHit == kInsert);
- break;
-
- case kBeginning:
- case kEnd:
- info->lineStart = (itemHit == kBeginning);
- break;
-
- case kSelectionOnly:
- info->selOnly = !info->selOnly;
- break;
- }
-
- transformSetButtons(dialog, info);
- break;
-
- case kDlgClose:
- GetDialogItem(dialog, kTransformString, &type, &hdl, &r);
- GetIText(hdl, info->transformStr);
- break;
- }
-
- return(-1);
- }
-
- void main(
- ExternalCallbackBlock *callbacks,
- WindowRef window
- )
- {
- long saved_a4;
- transformInfo info;
- long length = sizeof(info);
- long x, anchor, end;
- short itemHit;
-
- saved_a4 = SetCurrentA4();
-
- extGetSelection(callbacks, &anchor, &end);
-
- if (anchor > end) {
- x = anchor; anchor = end; end = x;
- }
-
- /*
- * Set up our default settings
- */
-
- extGetPreferences(callbacks, 'Prfx', &info, &length);
-
- if (length <= 0) {
-
- info.insert = true;
- info.lineStart = true;
-
- BlockMove("\p> ", info.transformStr, 3);
- }
-
- info.selOnly = (anchor != end);
-
- extDisplayDialog(callbacks, 128, transformHandler, &info, &itemHit);
-
- if (itemHit == kOk) {
-
- /*
- * Save our settings for next time
- */
-
- length = sizeof(info);
- extSetPreferences(callbacks, 'Prfx', &info, &length);
- }
-
- if (itemHit == kOk && info.transformStr[0] != 0) {
-
- long startLine = 1, endLine;
- long oldStart;
-
- /*
- * Determine the line to operate on
- */
-
- endLine = extLineCount(callbacks);
-
- if (info.selOnly) {
- startLine = extLineFromPosition(callbacks, anchor);
- endLine = extLineFromPosition(callbacks, end);
- }
-
- oldStart = startLine - 1;
-
- extStartProgress(callbacks, (Char *) "\pChanging Lines...",
- endLine - startLine + 1, true);
-
- /*
- * Now process all the lines
- */
-
- if (info.lineStart) {
-
- for (; startLine <= endLine; startLine++) {
-
- if (extDoProgress(callbacks, startLine - oldStart))
- break;
-
- anchor = extLineToPosition(callbacks, startLine);
-
- if (info.insert) {
- extSetSelection(callbacks, anchor, anchor);
- extInsert(callbacks, info.transformStr + 1, info.transformStr[0]);
- }
- else {
-
- if (extFindText(callbacks, (unsigned char *) info.transformStr + 1,
- info.transformStr[0],
- anchor, nil, nil, false, false) >= 0) {
-
- extSetSelection(callbacks, anchor, anchor + info.transformStr[0]);
- extDelete(callbacks);
- }
- }
- }
- }
-
- else {
-
- for (; startLine < endLine; startLine++) {
-
- if (extDoProgress(callbacks, startLine - oldStart))
- break;
-
- anchor = extLineEnd(callbacks, startLine);
-
- if (info.insert) {
- extSetSelection(callbacks, anchor, anchor);
- extInsert(callbacks, info.transformStr + 1, info.transformStr[0]);
- }
- else {
-
- if (extFindText(callbacks, (unsigned char *) info.transformStr + 1,
- info.transformStr[0],
- anchor - info.transformStr[0],
- nil, nil, false, false) >= 0) {
-
- extSetSelection(callbacks, anchor - info.transformStr[0],
- anchor);
- extDelete(callbacks);
- }
- }
- }
- }
-
- extDoneProgress(callbacks);
- }
-
- SetA4(saved_a4);
- }
-